<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/core.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/db.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/xmlfeed.php";

if (!isset ($_GET['p']))
{
  $install = 1;
  include "{$_SERVER['DOCUMENT_ROOT']}/includes/install.php";
}

include "{$_SERVER['DOCUMENT_ROOT']}/includes/login.php";

/* Show the login form? */
$showlogin = $login['username'] == "" && $createadmin != 1 ? 1 : 0;
if (!$showlogin && !isset ($_GET['p']) && $createadmin != 1)
{
  header ("location: ./?p=site");
  exit;
}

if (!isset ($_GET['p']))
  $_GET['p'] = "login";

if (!isset ($createadmin))
  $createadmin = 0;

$connection = db_open ();

/* Get the relevant pid */
if (isset ($_GET['guid']))
{
  $result = mysql_query ("SELECT id FROM ".db_maketablename ($table_pages_review)." WHERE guid LIKE '{$_GET['guid']}' ORDER BY version DESC LIMIT 1");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      $pid = $row['id'];
    }
  mysql_free_result ($result);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:php="http://php.net/xsl" xml:lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="robots" content="noindex, nofollow" />
    <title>Mirasol CMS</title>

    <script type="text/javascript"> 
      //<![CDATA[
        function externalLinks ()
        {
          if (!document.getElementsByTagName) return;
          var anchors = document.getElementsByTagName ("a");
          for (var i = 0; i < anchors.length; i++)
            {
              var anchor = anchors[i];
              if (anchor.getAttribute ("href") && anchor.getAttribute ("rel") == "external")
                anchor.target = "_blank";
            }
        }
        window.onload = externalLinks;
      //]]>
    </script>
		
		<link type="text/css" href="./styles/overcast/jquery-ui.css" rel="Stylesheet" />
    <script type="text/javascript" src="./scripts/jquery.min.js"></script>
    <script type="text/javascript" src="./scripts/jquery-ui.min.js"></script>
		<script type="text/javascript">
		  $(document).ready (function ()
			  {
<?php
if ($_GET['p'] == "site" && isset ($_GET['guid']))
{
?>
					// Date picker for Publish tab in page editor
					$("#startdate").datepicker ({ showOn: "focus", showAnim: "fade" });
					$("#enddate").datepicker ({ showOn: "focus", showAnim: "fade" });
<?php
} /* $_GET['p'] == "site" && isset ($_GET['guid']) */
?>
					// Buttons
					$("input:button, input:submit", ".operate" ).button ();
				});
		</script>
<?php
if ((!isset ($_GET['p']) || $_GET['p'] == "site") && isset ($_GET['guid']))
{
  $tbl_pages = db_maketablename ($table_pages_review);
  $tbl_templates = db_maketablename ($table_templates);
  $result = mysql_query ("SELECT $tbl_templates.fields FROM $tbl_pages LEFT JOIN $tbl_templates ON $tbl_templates.id=$tbl_pages.template WHERE $tbl_pages.id='$pid'");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
			$fields = $row['fields'];
		}
	mysql_free_result ($result);
	
	$num_html = xmlfeed_numhtmlfields ($fields);
	$i = 1;
	while ($i <= $num_html)
	  {
			if ($i == 1)
			  $elements = "htmleditor$i";
			else
	      $elements .= ",htmleditor$i";
			$i++;
		}
?>
		<!-- MD5 implementation -->
		<script src="scripts/md5-min.js" type="text/javascript"></script>
    <script type="text/javascript">
			//<![CDATA[
			  function editorchanged ()
				{
					document.getElementById ("btnsave").style.display = "block";
					document.getElementById ("btnpreview").style.display = "none";
				}

        function needsave (check, hash)
        {
          check = hex_md5 (check);
					if (check != hash)
					  editorchanged ();
        }
				
				function preview (guid, version)
				{
					window.open ("/?guid="+guid+"&ver="+version, "preview");
				}
				
				function toogle_noend (state)
				{
					document.getElementById ("enddate").disabled = state;
					document.getElementById ("endtime").disabled = state;
					document.getElementById ("endpm").disabled = state;
					document.getElementById ("endnow").disabled = state;
				}
      //]]>
    </script>
		
<?php
	/* Use TinyMCE at all? */
	if ($num_html > 0)
	  {
?>
    <!-- TinyMCE stuff -->
    <script type="text/javascript" src="scripts/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript">
     //<![CDATA[
        tinyMCE.init ({
          theme : "advanced",
          mode : "exact",
          elements : "<?=$elements?>",
          
          theme_advanced_buttons1 : "undo,redo,|,cut,copy,paste,charmap,|,justifyleft,justifycenter,justifyright,justifyfull,|,visualaid,code",
      		theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,bullist,numlist,|,link,unlink",
		      theme_advanced_buttons3 : "",
          
          theme_advanced_toolbar_location : "top",
          theme_advanced_toolbar_align : "left",
		      theme_advanced_statusbar_location : "bottom",
					
					setup : function (ed)
					{
						ed.onKeyDown.add (function (ed, l) { editorchanged (); });
					}						
        });
      //]]>
    </script>
<?php
		}
}
?>
<?php
if ($_GET['p'] == "site")
{
?>
    <script type="text/javascript">
      //<![CDATA[
        function delpage (path, pid)
        {
          if (confirm ("You are about to delete:\n\n\t"+path+"\n\nAre you sure?"))
            window.location = "../includes/delpage.php?pid="+pid;
        }
      //]]>
    </script>
<?php
} /* $_GET['p'] == "site" */
?>
<?php
if (($_GET['p'] == "files") || ($_GET['p'] == "templates" && isset ($_GET['tid'])) || ($_GET['p'] == "site" && isset ($_GET['guid'])))
{
?>
		<script type="text/javascript">
			//<![CDATA[
				function opendialog (url, w, h)
					{
						var left = (screen.width / 2) - (w / 2);
						var top = (screen.height / 2) - (h / 2);
						window.open (url, "Edit", 'toolbar=no, location=no, status=no, menubar=no, resizable=no, width='+w+', height='+h+', top='+top+', left='+left);
					}
			//]]>
		</script>
<?php
} /* $_GET['p'] == "files" && isset ($_GET['dt']) */
?>
<?php
if ($_GET['p'] == "files" && isset ($_GET['dt']))
{
?>
    <script type="text/javascript">
      //<![CDATA[
        function delfile (filename, fid, dt)
        {
          if (confirm ("You are about to delete:\n\n\t"+filename+"\n\nAre you sure?"))
            window.location = "../includes/delfile.php?fid="+fid+"&dt="+dt;
        }
      //]]>
    </script>
<?php
} /* $_GET['p'] == "files" && isset ($_GET['dt']) */
?>
<?php
if (($_GET['p'] == "styles" && isset ($_GET['sid'])) || ($_GET['p'] == "templates" && isset ($_GET['tid'])) || ($_GET['p'] == "templates" && isset ($_GET['slid'])))
{
?>
    <link rel="stylesheet" href="scripts/codemirror/lib/codemirror.css" type="text/css" />
    <script type="text/javascript" src="scripts/codemirror/lib/codemirror.js"></script>
    <script type="text/javascript" src="scripts/codemirror/<?=$_GET['p'] == "styles" ? "css.js" : "xmlpure.js" ?>"></script>
    <link rel="stylesheet" href="scripts/codemirror/theme/default.css" type="text/css" />
    <style type="text/css">
      .CodeMirror {
        border: 1px solid #aaa;
        background-color: #fff;
      }
    </style>
<?php
}
?>
<?php
if ($_GET['p'] == "styles")
{
?>
    <script type="text/javascript">
      //<![CDATA[
        function delstyle (name, sid)
        {
          if (confirm ("You are about to delete:\n\n\t"+name+"\n\nAre you sure?"))
            window.location = "../includes/delstyle.php?sid="+sid;
        }
      //]]>
    </script>
<?php
}
?>
<?php
if ($_GET['p'] == "templates")
{
?>
    <script type="text/javascript">
      //<![CDATA[
        function delsublayout (name, slid)
        {
          if (confirm ("You are about to delete:\n\n\t"+name+"\n\nAre you sure?"))
            window.location = "../includes/delsublayout.php?slid="+slid;
        }

        function deltemplate (name, tid)
        {
          if (confirm ("You are about to delete:\n\n\t"+name+"\n\nAre you sure?"))
            window.location = "../includes/deltemplate.php?tid="+tid;
        }
      //]]>
    </script>
<?php
}
?>
<?php
if (($_GET['p'] == "site"  && isset ($_GET['guid'])) || ($_GET['p'] == "templates" && isset ($_GET['tid'])))
{
?>
		<script type="text/javascript">
		  // <![CDATA[
				function switchtabs (tab)
				{
					if (tab == "tab1")
						{
							document.getElementById ("tab1").style.display = "block";
							document.getElementById ("tab2").style.display = "none";
							document.getElementById ("tab3").style.display = "none";
							document.getElementById ("show").value = "1";
						}
					else if (tab == "tab2")
						{
							document.getElementById ("tab1").style.display = "none";
							document.getElementById ("tab2").style.display = "block";
							document.getElementById ("tab3").style.display = "none";
							document.getElementById ("show").value = "2";
						}
					else if (tab == "tab3")
						{
							document.getElementById ("tab1").style.display = "none";
							document.getElementById ("tab2").style.display = "none";
							document.getElementById ("tab3").style.display = "block";
							document.getElementById ("show").value = "3";
						}
				}
			// ]]>
		</script>
		
<?php
} /* ($_GET['p'] == "site"  && isset ($_GET['guid'])) || ($_GET['p'] == "templates" && isset ($_GET['tid'])) */
?>
    <link rel="icon" href="./favicon.ico" type="image/x-icon" /> 
    <link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./styles/mirasol.css" type="text/css" media="screen" />
  </head>

  <body>
    <div id="menu">
			<div id="logo"><img src="images/logo_small_bw.png" alt="<?=$app_name ?>" /></div>
<?php
if ($showlogin == 1)
{
?>
      <ul>
        <li><span><?=$app_name ?> <?=$app_version ?></span></li>
      </ul>
<?php
}
else
{
?>
      <div class="alignright">
        <ul>
          <li><a href="./?p=about">About</a></li>
          <li><a href="./logoff.php">Log off</a></li>
        </ul>
      </div>
      <ul>
        <li><a href="./?p=site">Site</a></li>
        <li><a href="./?p=templates">Templates</a></li>
        <li><a href="./?p=styles">Styles</a></li>
        <li><a href="./?p=files">Files</a></li>
        <li><a href="#">Users</a></li>
        <li><a href="./?p=settings">Settings</a></li>
      </ul>
<?php
}
?>
    </div>
    
    <div id="content">
      <?php
        $loadsubpage = 1;
        
        if ($createadmin == 1)
          include getcurrentpath ()."/createadmin.php";
        else if ($showlogin == 1)
          include getcurrentpath ()."/login.php";
        else if (!isset ($_GET['p']) || $_GET['p'] == "site")
          include getcurrentpath ()."/site.php";
        else if ($_GET['p'] == "about")
          include getcurrentpath ()."/about.php";
        else if ($_GET['p'] == "files")
          include getcurrentpath ()."/files.php";
        else if ($_GET['p'] == "settings")
          include getcurrentpath ()."/settings.php";
        else if ($_GET['p'] == "styles")
          include getcurrentpath ()."/styles.php";
        else if ($_GET['p'] == "templates")
          include getcurrentpath ()."/templates.php";
      ?>
    </div>
    
    <div id="footer">
      <p><?=$app_name?> (Ver. <?=$app_version?>)<br /><?=$app_copyright?><br /><?=$app_license?></p>
    </div>
  </body>
</html>
<?php
db_close ($connection);
?>
